/**
 * 
 */
package org.pump.client.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.LinkedBlockingQueue;

import org.pump.client.codec.EventData;
import org.pump.client.filter.FilterTube;
import org.pump.client.pool.PumpPool;
import org.pump.client.util.MyConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author liudong03
 *
 */
public class MyPump {
	private static final Logger mylog = LoggerFactory
			.getLogger(MyPump.class);
	public static Version myversion = new Version();
	
	MyProperty conf = new MyProperty();
	MyClient client = new MyClient();
	PumpPool eventPool;
	FilterTube mytube;
	
	public MyPump()
	{
		//filter tube
		this.mytube = new FilterTube();
	}
	/**
	 * @param args
	 */
	public int start(String absolutepath) {
		//init property file
		int ret = conf.init(absolutepath);
		if(ret != MyConstant.SUCC)
		{
			mylog.error("init configuration file fail");
			return ret;
		}
		//init event queue
		this.eventPool = new PumpPool(conf.queuesize, conf);
		this.eventPool.setFilterTube(mytube);
		//start client
		client.init(conf,this.eventPool);
		ret = client.start();
		if(ret != MyConstant.SUCC)
		{
			mylog.error("start client fail");
			return ret;
		}
		
		return MyConstant.SUCC;
	}

	
	
	public void stop()
	{
		this.client.stop();
	}
	
	public void addBlackEvent(short eventtype)
	{
		this.mytube.addBlackEvent(eventtype);
	}
	
	public void addWhiteTable(String dbname,String tablename)
	{
		this.mytube.addWhiteTable(dbname, tablename);
	}
	/**
	 * @return the eventPool
	 */
	public PumpPool getEventPool() {
		return eventPool;
	}
	
	public boolean commit()
	{
		if(!this.eventPool.commit())
		{
			mylog.error("eventpool commit fail");
			return false;
		}
		return true;
	}
	
	public EventData getEvent()
	{
		return this.eventPool.take(1000);
	}

}
